package com.autocar.reader.net;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import android.os.SystemClock;
import android.util.Log;

public class AsynThreadManager {

	private static final SynchronousQueue<Runnable> synrun = new SynchronousQueue<Runnable>();

	private static final ThreadPoolExecutor executorService = new ThreadPoolExecutor(
			5, Integer.MAX_VALUE, 30L, TimeUnit.SECONDS, synrun,
			new DiscardOldestPolicys());

	public static void doTask(Runnable run) {
		executorService.execute(run);
	}

	public static class DiscardOldestPolicys implements
			RejectedExecutionHandler {

		public DiscardOldestPolicys() {
		}

		public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
			if (!e.isShutdown()) {
				e.getQueue().poll();
				e.execute(r);
			}
		}
	}

	public static void sendLog() {

		new Thread() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Log.e("mg", "--->send");
				SystemClock.sleep(1000);
				Log.e("mg", "--->ok over...");
			}
		}.start();
	}

	private static final ExecutorService singleThread = Executors
			.newSingleThreadExecutor();

	public static void execuSingleTask(Runnable run) {
		singleThread.execute(run);
	}
}
